# -*- coding: utf-8 -*-

'''
Created on 1 dec. 2009

@author: vincent.vanghelle
'''

from django.shortcuts import render_to_response,redirect, get_object_or_404
from mmorganizer.characters.forms import CharacterCreationForm,RosterCreationForm
from mmorganizer.games.models import Class
from mmorganizer.characters.models import Personnage, Roster
from django.contrib.auth.decorators import login_required

@login_required
def create(request):
    if request.POST:
        perso = Personnage()
        form = CharacterCreationForm(request.POST,perso)
        if form.is_valid():
            form.saveData(commit=True,user=request.user)
            return redirect('/characters/list')

    else:
        form = CharacterCreationForm()
        form.specifierIdJeu(request.user.get_profile().game.id)
    return render_to_response('characters/create.html',{'form':form, 'user':request.user})

@login_required
def get_associated_roles(request):
    if request.is_ajax() and request.method == 'POST':
        idclass = request.POST.get('id_classe', '')
        if (idclass != ''):
            roles = Class.objects.get(id=request.POST.get('id_classe', '')).roles.all()
    return render_to_response('characters/ajax_get_associated_roles.html', locals())


@login_required
def list(request):
    persos = Personnage.objects.filter(game__id=request.user.get_profile().game.id).filter(user__id=request.user.id)
    return render_to_response('characters/list.html',{'persos':persos, 'user':request.user})

@login_required
def list_roster(request):
    rosters = Roster.objects.filter(responsable__id=request.user.id).filter(game__id=request.user.get_profile().game.id)
    return render_to_response('characters/list_roster.html',{'rosters':rosters, 'user':request.user})

@login_required
def create_roster(request):
    if request.POST:
        roster = Roster()
        form = RosterCreationForm(request.POST,roster)
        if form.is_valid():
            form.saveData(commit=True,responsable=request.user)
            return redirect('/characters/roster/list')
    else:
        form = RosterCreationForm()
        form.specifierIdJeu(request.user.get_profile().game.id)
    return render_to_response('characters/create.html',{'form':form, 'user':request.user})


@login_required
def list_roster_for_character(request,character_id):
    perso = get_object_or_404(Personnage,id=character_id)
    if(perso.user != request.user):
        return redirect('/accessDenied')
    associatedRosters = perso.rosters.all()
    associatedRostersWaiting = perso.rostersEnAttente.all()
    return render_to_response('characters/list_roster.html',{'associatedRosters':associatedRosters,'associatedRostersWaiting':associatedRostersWaiting,'selectedCharacter':perso, 'user':request.user})

@login_required
def select_roster_to_join(request,character_id):
    perso = get_object_or_404(Personnage,id=character_id)
    if(perso.user != request.user):
        return redirect('/accessDenied')
    availableRosters = Roster.objects.filter(serveur = perso.serveur)
    return render_to_response('characters/join_roster.html',{'availableRosters':availableRosters,'selectedCharacter':perso, 'user':request.user})

@login_required
def join_roster(request,character_id,roster_id):
    perso = get_object_or_404(Personnage,id=character_id)
    if(perso.user != request.user):
        return redirect('/accessDenied')
    roster = get_object_or_404(Roster,id=roster_id)
    perso = get_object_or_404(Personnage,id=character_id)
    if (perso not in roster.membres.all()):
        roster.membresEnAttente.add(perso)
    return redirect('/characters/list/')

@login_required
def list_membres_roster(request,roster_id):
    roster = get_object_or_404(Roster,id=roster_id)
    if(request.user!=roster.responsable):
        return redirect('/accessDenied')
    persos = roster.membres.all()
    persosAttente = roster.membresEnAttente.all()
    return render_to_response('characters/membres_roster.html',{'roster':roster,'persos':persos,'persosAttente':persosAttente, 'user':request.user})

@login_required
def valider_membreEnAttente_roster(request,roster_id,character_id,action):
    roster = get_object_or_404(Roster,id=roster_id)
    if(request.user!=roster.responsable):
        return redirect('/accessDenied')
    perso = Personnage.objects.get(id=character_id)
    if (action=="validate"):
        roster.membresEnAttente.remove(perso)
        roster.membres.add(perso)
    if (action=="reject"):
        roster.membresEnAttente.remove(perso)
    return redirect('/characters/roster/members/'+str(roster.id))

@login_required
def supprimer_membre_roster(request,roster_id,character_id):
    roster = get_object_or_404(Roster,id=roster_id)
    if(request.user!=roster.responsable):
        return redirect('/accessDenied')
    perso = Personnage.objects.get(id=character_id)
    roster.membres.remove(perso)
    return redirect('/characters/roster/members/'+str(roster.id))

@login_required
def select_personnage(request,personnage_id):
    perso = get_object_or_404(Personnage,id=personnage_id)
    if (request.user!=perso.user):
        return redirect('/accessDenied')
    profil = request.user.get_profile()
    profil.defaultPersonnage = perso
    profil.save()
    next = request.GET.get('next','')
    if next == '':
        redirect('/index')
    return redirect(next)

